//
//  KDMainPage.m
//  KDExample
//
//  Created by 叶青 on 2017/3/7.
//  Copyright © 2017年 naruto. All rights reserved.
//

#import "KDMainPage.h"

@interface KDMainPage ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray            *listMenuSource;
@property (nonatomic, strong) UITableView               *tbMenu;
@end

@implementation KDMainPage
#pragma mark - system
+(void)load
{
    [kNBCURLRouterInstance registerScheme:@"KDMainPage" scheme:@"home"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"首页"];
    [self.view addSubview:self.tbMenu];
}

-(void)dealloc
{
    BCLogDealloc();
}


#pragma mark - getter
-(UITableView *)tbMenu
{
    if(!_tbMenu){
        _tbMenu = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kBCSCREEN_WIDTH,kBCSCREEN_HEIGHT) style:UITableViewStylePlain];
        _tbMenu.dataSource = self;
        _tbMenu.delegate = self;
        _tbMenu.allowsMultipleSelection = NO;
        _tbMenu.backgroundColor = [UIColor whiteColor];
        _tbMenu.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    }
    return _tbMenu;
}
-(NSMutableArray *)listMenuSource
{
    if(!_listMenuSource){
        _listMenuSource = [[NSMutableArray alloc] initWithCapacity:0];
        [_listMenuSource addObject:@{@"id":@"health",@"name":@"健康",@"cls":@"health"}];
        [_listMenuSource addObject:@{@"id":@"lock",@"name":@"锁",@"cls":@"testpage"}];
        [_listMenuSource addObject:@{@"id":@"isa",@"name":@"isa",@"cls":@"isapage"}];
        [_listMenuSource addObject:@{@"id":@"swift",@"name":@"swift",@"cls":@"testpage"}];
        [_listMenuSource addObject:@{@"id":@"runtime",@"name":@"runtime",@"cls":@"testpage"}];
        [_listMenuSource addObject:@{@"id":@"vpn",@"name":@"vpn",@"cls":@"vpnpage"}];
        [_listMenuSource addObject:@{@"id":@"dd vpn",@"name":@"dd vpn",@"cls":@"ddvpnpage"}];
        [_listMenuSource addObject:@{@"id":@"copy strong",@"name":@"copy strong",@"cls":@"copypage"}];
        [_listMenuSource addObject:@{@"id":@"category",@"name":@"category",@"cls":@"categorypage"}];
        [_listMenuSource addObject:@{@"id":@"thread",@"name":@"thread",@"cls":@"threadpage"}];
    }
    return _listMenuSource;
}


#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listMenuSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.listMenuSource.count <= indexPath.row){
        return nil;
    }
    NSDictionary *functionItem = [self.listMenuSource objectAtIndex:indexPath.row];
    static NSString *kCellIdentifier = @"kBPFunctionListTableViewCellIdentifierKey";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
    }
    cell.textLabel.text = functionItem[@"name"];
    return cell;
}


#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(unselectRow) object:nil];
    [self performSelector:@selector(unselectRow) withObject:nil afterDelay:0.2];
    if(self.listMenuSource.count <= indexPath.row){
        return ;
    }
    
    //跳转h5
    NSDictionary *functionItem = [self.listMenuSource objectAtIndex:indexPath.row];
    NSString *strCls = functionItem[@"cls"];
    NSString *strName = functionItem[@"name"];
    if(strCls.length==0){
        return;
    }
    strCls = [NSString stringWithFormat:@"%@?name=%@",strCls,strName];
    [kNBCURLRouterInstance pushURLString:strCls animated:YES];
    
}
-(void)unselectRow
{
    [self.tbMenu deselectRowAtIndexPath:[self.tbMenu indexPathForSelectedRow] animated:YES];
}

@end
